package com.thinkgem.jeesite.frame.pm.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.thinkgem.jeesite.frame.pm.entity.PmDataDict;
import com.thinkgem.jeesite.frame.pm.service.PmDataDictService;

/**
 * 数据字典Controller
 *
 * @author kongxiangsheng
 * @version 2018-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/pm/pmDataDict")
public class PmDataDictController extends BaseController {
    @Autowired
    private PmDataDictService pmDataDictService;

    @RequiresPermissions("pm:pmDataDict:view")
    @RequestMapping(value = {"list", ""})
    public String list(PmDataDict pmDataDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        BasePage<PmDataDict> page = new BasePage<PmDataDict>(request, response);
        pmDataDictService.page(page, new QueryWrapper<PmDataDict>(pmDataDict));

        model.addAttribute("page", page);
        return "frame/pm/PmDataDict/list";
    }

    @RequiresPermissions("pm:pmDataDict:view")
    @RequestMapping(value = "form")
    public String form(PmDataDict pmDataDict, Model model) {
        if (StringUtils.isNotBlank(pmDataDict.getId())) {
            pmDataDict = pmDataDictService.getWithChildren(pmDataDict.getId());
        }
        model.addAttribute("pmDataDict", pmDataDict);
        return "frame/pm/PmDataDict/form";
    }

    @RequiresPermissions("pm:pmDataDict:edit")
    @RequestMapping(value = "save")
    public String save(PmDataDict pmDataDict, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, pmDataDict)) {
            return form(pmDataDict, model);
        }
        pmDataDictService.saveWithChildren(pmDataDict);
        addMessage(redirectAttributes, "保存数据字典成功");
        return "redirect:" + adminPath + "/pm/pmDataDict/?repage";
    }

    @RequiresPermissions("pm:pmDataDict:edit")
    @RequestMapping(value = "delete")
    public String delete(PmDataDict pmDataDict, RedirectAttributes redirectAttributes) {
        pmDataDictService.deleteWithChildren(pmDataDict);
        addMessage(redirectAttributes, "删除数据字典成功");
        return "redirect:" + adminPath + "/pm/pmDataDict/?repage";
    }

}